/**
 * 全局组件注册
 */
import Vue from 'vue';
import CountTo from 'vue-count-to';

const requireComponent = require.context(
  '.',
  true,
  /^\.\/(form|page|charts|part|route)\/\S+\.vue$/,
);
requireComponent.keys().forEach((pathName) => {
  const componentPaths = pathName.split('/');
  let componentName;
  // 通过正则获取并注册组件
  // 1. ./组件类型/组件名/index.vue 组件
  // 2. ./组件名/index.vue 组件
  if (
    (componentPaths.length === 3 || componentPaths.length === 4) &&
    componentPaths[componentPaths.length - 1] === 'index.vue'
  ) {
    componentName = componentPaths[componentPaths.length - 2];
  }

  if (componentName) {
    const componentConfig = requireComponent(pathName);
    Vue.component(componentName, componentConfig.default || componentConfig);
  }
});

// 使用方法 <count-to :end-val="value" />
// 详细文档：http://panjiachen.github.io/countTo/demo/
Vue.component('CountTo', CountTo);
